package model;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class ConnectToServer {
	URL url;
	URLConnection conn;
	String host="localhost";
	UserInfo userInfo = UserInfo.getInstance();
	
	public boolean checkUser(String userName, String pass){		
		try {			
			url = new URL("http://"+host+":12345/isClientExistServlet?userName="+userName+"&pass="+pass);
			conn = url.openConnection();
			conn.setAllowUserInteraction(true);
			//conn.setDoOutput(true);
		    ObjectInputStream inputStream = new ObjectInputStream(conn.getInputStream());
		    boolean flag = (Boolean)inputStream.readObject();
		    inputStream.close();
		    return flag;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return false;
	}
	
	public boolean createAccount(String userName, String pass, String email, String computerName, String path, String folderName){		
		try {			
			url = new URL("http://"+host+":12345/CreateAccountServlet?userName="+userName+"&pass="+pass+"&email="+email+"&compName="+computerName+"&path="+path+"&boxName="+folderName);
			conn = url.openConnection();
			conn.setAllowUserInteraction(true);
			//conn.setDoOutput(true);
			ObjectInputStream inputStream = new ObjectInputStream(conn.getInputStream());
		    boolean flag = (Boolean)inputStream.readObject();
		    inputStream.close();
		    return flag;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
	    return true;
	}
	/*public boolean checkUser2(String userName, String pass){		
		try {
			
			url = new URL("http://"+host+":12345/isExist");
			
			conn = url.openConnection();
			conn.setAllowUserInteraction(true);
			conn.setDoOutput(true);
			
			ObjectOutputStream outputStream = new ObjectOutputStream(conn.getOutputStream());
		    outputStream.writeObject(new StartComunication(userName, pass,userInfo.getComputerName()));
		    outputStream.flush();
		    outputStream.close();
		    ObjectInputStream inputStream = new ObjectInputStream(conn.getInputStream());
		    Message message = (Message)inputStream.readObject();
		    if(message.getType()==MessageType.INFO)
		    	return true;
		    else if(message.getType()==MessageType.STOP_COMUNICATION)
		    	return false;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return false;
	}*/
	
	/*public boolean checkUserPass(String userName, String pass){
		try {
			url = new URL("http://"+host+":12345/validate");
			conn = url.openConnection();
			conn.setAllowUserInteraction(true);
			conn.setDoOutput(true);
			ObjectOutputStream outputStream = new ObjectOutputStream(conn.getOutputStream());
		    outputStream.writeObject(new StartComunication(userName, pass,userInfo.getComputerName()));
		    outputStream.flush();
		    outputStream.close();
		    ObjectInputStream inputStream = new ObjectInputStream(conn.getInputStream());
		    Message message = (Message)inputStream.readObject();
		    if(message.getType()==MessageType.INFO){
		    	Info infoMessage = (Info)message;
		    	UserInfo userInfo = UserInfo.getInstance();
		    	userInfo.setUserName(infoMessage.getUserName());
		    	userInfo.setPass(infoMessage.getPass());
		    	userInfo.setEmail(infoMessage.getEmail());
		    	userInfo.setBoxesList(infoMessage.getBoxesList());
		    	return true;
		    }
		    else if(message.getType()==MessageType.STOP_COMUNICATION)
		    	return false;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    return false;
	}*/
}
